﻿Imports System.Collections.Generic

Namespace DoFactory.GangOfFour.Prototype.RealWorld
    ''' <summary>
    ''' MainApp startup class for Real-World
    ''' Prototype Design Pattern.
    ''' </summary>
    Public Class MainApp
        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()
            Dim colormanager As New ColorManager()

            ' Initialize with standard colors
            colormanager("red") = New Color(255, 0, 0)

            colormanager("green") = New Color(0, 255, 0)

            colormanager("blue") = New Color(0, 0, 255)

            ' User adds personalized colors
            colormanager("angry") = New Color(255, 54, 0)

            colormanager("peace") = New Color(128, 211, 128)

            colormanager("flame") = New Color(211, 34, 20)

            ' User clones selected colors
            Dim color1 As Color = TryCast(colormanager("red").Clone(), Color)

            Dim color2 As Color = TryCast(colormanager("peace").Clone(), Color)

            Dim color3 As Color = TryCast(colormanager("flame").Clone(), Color)

        End Sub

    End Class

    ''' <summary>
    ''' The 'Prototype' abstract class
    ''' </summary>
    MustInherit Class ColorPrototype

        Public MustOverride Function Clone() As ColorPrototype

    End Class

    ''' <summary>
    ''' The 'ConcretePrototype' class
    ''' </summary>
    Class Color
        Inherits ColorPrototype

        Private _red As Integer

        Private _green As Integer

        Private _blue As Integer

        ' Constructor
        Public Sub New(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer)
            Me._red = red
            Me._green = green
            Me._blue = blue
        End Sub

        ' Create a shallow copy
        Public Overrides Function Clone() As ColorPrototype
            Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue)
            Return TryCast(Me.MemberwiseClone(), ColorPrototype)
        End Function

    End Class

    ''' <summary>
    ''' Prototype manager
    ''' </summary>
    Class ColorManager

        Private _colors As New Dictionary(Of String, ColorPrototype)()

        ' Indexer
        Default Public Property Item(ByVal key As String) As ColorPrototype
            Get
                Return _colors(key)
            End Get
            Set(ByVal value As ColorPrototype)
                _colors.Add(key, value)
            End Set
        End Property

    End Class

End Namespace
